//
//  ANMineOrderController.m
//  WeiYiMei
//
//  Created by 李叶夫 on 2019/4/14.
//  Copyright © 2019年 liyefu. All rights reserved.
//

#import "ANMineOrderController.h"
#import "MineOrderSegView.h"
#import "ANMineOrderListController.h"

@interface ANMineOrderController ()<UIScrollViewDelegate>

@property (nonatomic, strong) MineOrderSegView *segView;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation ANMineOrderController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的订单";
   
}

- (void)setupSubViews
{
    self.segView = [[MineOrderSegView alloc] init];
    [self.view addSubview:self.segView];
    
    [self.segView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.equalTo(@44);
    }];
    
    CGFloat h = kScreenH-kStatusAndNavBarHeight-44;
    self.scrollView = [[UIScrollView alloc] init];
    self.scrollView = self.scrollView;
    self.scrollView.delegate  = self;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.contentSize = CGSizeMake(kScreenW*4, h);
    self.scrollView.frame= CGRectMake(0, 44, kScreenW, h);
    [self.view addSubview:self.scrollView];
    
    self.segView.scrollView = self.scrollView;
    
    ANMineOrderListController *vc1 = [[ANMineOrderListController alloc] init];
    vc1.orderState = OrderStateAll;
    [self addChildViewController:vc1];
    vc1.view.frame = CGRectMake(0, 0, kScreenW, h);
    [self.scrollView addSubview:vc1.view];
    

    
    ANMineOrderListController *vc2 = [[ANMineOrderListController alloc] init];
    vc2.orderState = OrderStateNoPay;
    [self addChildViewController:vc2];
    vc2.view.frame = CGRectMake(kScreenW, 0, kScreenW, h);
    [self.scrollView addSubview:vc2.view];
    
    
    ANMineOrderListController *vc3 = [[ANMineOrderListController alloc] init];
    vc3.orderState = OrderStateNoReceiving;
    [self addChildViewController:vc3];
    vc3.view.frame = CGRectMake(kScreenW*2, 0, kScreenW, h);
    [self.scrollView addSubview:vc3.view];
    
    
    ANMineOrderListController *vc4 = [[ANMineOrderListController alloc] init];
    vc4.orderState = OrderStateComplete;
    [self addChildViewController:vc4];
    vc4.view.frame = CGRectMake(kScreenW*3, 0, kScreenW, h);
    [self.scrollView addSubview:vc4.view];
    
    
    WeakSelf
    self.segView.selectIndex = ^(NSInteger index) {
        [weakSelf.scrollView scrollRectToVisible:CGRectMake(kScreenW*index, 0, weakSelf.scrollView.width, weakSelf.scrollView.height) animated:YES];
        [weakSelf refreshLoad:index];
    };
    
}

- (void)refreshLoad:(NSInteger)page
{
    if (self.childViewControllers.count < page + 1) {
        return;
    }
    ANMineOrderListController *vc = self.childViewControllers[page];
    [vc refreshLoad];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.segView.offSet = scrollView.contentOffset.x;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int page = (scrollView.contentOffset.x / kScreenW);
    [self refreshLoad:page];
}

@end
